<template>
  <div class="root">
    <div class="title2">添加编号为{{ stuno }}的学生信息</div>
    <el-form
      class="form"
      label-width="120px"
      style="--el-color-white: transparent"
    >
      <el-form-item label="学生姓名" class="item">
        <el-input v-model="stuname"></el-input>
      </el-form-item>
      <el-form-item label="学生年龄" class="item">
        <el-input v-model="age"></el-input>
      </el-form-item>
      <el-form-item label="所在班号" class="item">
        <el-input-number
          v-model="classno"
          :min="1"
          :max="45"
        />
      </el-form-item>
      <el-form-item class="item">
        <el-button
          style="background-color: transparent; color: azure"
          @click="addStu"
          >添加</el-button
        >
        <el-button
          @click="cancel"
          style="background-color: transparent; color: azure"
          >取消</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
import { ElNotification } from 'element-plus'
export default {
  data() {
    return {
      stuno: this.$store.state.total + 1,
      classno: 1,
      stuname: "",
      age: ""
    };
  },
  methods: {
    addStu() {
      if (this.classno < 10) this.classno = "0" + this.classno;
      axios
        .get("http://localhost:8082/add", {
          params: {
            stuno: this.stuno,
            stuname: this.stuname,
            age: this.age,
            classno: this.classno,
          },
        })
        .then((res) => {
          console.log(res.data);
          if (res.data == "error") {
            ElNotification({
              title: "添加信息失败",
              message: "请检查相关信息填写是否符合要求",
              type: 'error',
              duration:2000
            });
          }
          else{
             ElNotification({
              title: "数据库变动",
              message: "成功添加学生信息",
              type: 'success',
              duration:2000
            });
          }
        });
    },
    cancel() {
      this.stuname = "";
      this.age = "";
      this.classno = "";
    }
  },
};
</script>

<style>
.form {
  width: 50%;
  margin: 25px auto;
}
.item .el-form-item__label {
  color: azure;
}
.item .el-input__inner {
  color: azure;
}
.title2 {
  margin-bottom: 30px;
  margin-top: 40px;
  color: azure;
  font-size: 20px;
}
</style>